<?php
/*
*	Kleophatra - Open Source Content Management System
*	------------------------------------------------------------------------
*	Copyright (C) 2007-2010, Sergio Milardovich <smilardovich@yahoo.com.ar>
*	This program is Free Software.
*
*	@package	Kleophatra
*	@subpackage	/lib
*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
*	@author		Sergio Milardovich <smilardovich@yahoo.com.ar>
*	@link		http://www.kleophatra.org
*/
	class kleomodules extends kleotemplate{

		function load_module($module, &$buff){
			if(!file_exists(KLEO_MODULES_DIR.$module.'/controllers/'.$module.'.php')){
				$this->apperror($module.'\'s controller does not exists', "common");
			}
			if(!file_exists(KLEO_MODULES_DIR.$module.'/models/data.php')){
				$this->apperror($module.'\'s model does not exists', 'common');
			}
			
			require_once KLEO_MODULES_DIR.$module.'/models/data.php';
			require_once KLEO_MODULES_DIR.$module.'/controllers/'.$module.'.php';

			if($this->is_active_module($module)){
				$module = new $module();
				$load_function = eval($load_function);
				$buff = $buff;
			} else {
				$this->tpl_set(array(
					"WEB_CONTENT" => L_INACTIVE_MODULE, 
					"BREADCRUMB" => false),
					false, $buff, $buff);
			}
			
		}
		function install_module($module){
			$module_file = KLEO_MODULES_DIR.$module.'/models/data.php';
			require_once $module_file;
			$model = $module.'model';
			$module_install = new $model();
			$module_install->install_module();
		}
		function remove_module($module){
			$module_file = KLEO_MODULES_DIR.$module.'/models/data.php';
			require_once $module_file;
			$model = $module.'model';
			$module_remove = new $model();
			$module_remove->remove_module();
		}
		function enable_module($module){
				$this->query("UPDATE `".KLEO_TABLE_PREFIX."modules` SET `status` =  1 WHERE `moid` = $module");
		}
		function disable_module($module){
				$this->query("UPDATE `".KLEO_TABLE_PREFIX."modules` SET `status` =  0 WHERE `moid` =$module");
		}
		function is_active_module($module){
			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."modules WHERE name = '$module'");
			if($this->num_rows($query)){
				return true;
			} else {
				return false;
			}
		}
		function get_module_name($id){
			$query = $this->query("SELECT name FROM ".KLEO_TABLE_PREFIX."modules WHERE moid = $id");
			$row = $this->fetch_array($query);
			return $row['name'];
		}
		function add_module($name,$key=0,$status=0){
			if($key == 0){
				$key = rand();
			}
			$this->query("INSERT INTO `".KLEO_TABLE_PREFIX."modules` (
					`moid` ,
					`name` ,
					`key` ,
					`status`
				) VALUES (
					NULL , '$name', '$key', '$status'
				);");
		}
	}


?>
